/*
 * @FileName: [SysParamterController.java]
 * 
 * @Package com.bcinfo.upp.admin.ac.controller
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com. All rights reserved.
 * 
 * This software is the confidential and proprietary information of BCINFO Technology Limited Company ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in accordance with the terms of the contract agreement you entered into with RKY.
 * 
 * $Rev: 45 $ $LastChangedDate: 2013-10-12 10:20:10 +0800 (星期六, 12 十月 2013) $ $LastChangedBy: wanggc $
 * 
 * @category bcinfo-upp
 * 
 * @version 1.1
 * 
 * @author $Author: wanggc $
 * 
 * Change History:[Formatter: author date description] <br/> 1 2 3
 */

package com.bcinfo.iccard.admin.sys.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bcinfo.iccard.admin.common.ModuleEnum;
import com.bcinfo.iccard.admin.common.ModuleEnum.SysParam;
import com.bcinfo.iccard.admin.controller.AbstractController;
import com.bcinfo.upp.exception.UppException;
import com.bcinfo.upp.sys.model.SysParameter;
import com.bcinfo.upp.sys.service.ConfigureService;
import com.bcinfo.upp.util.PaginationList;

/**
 * 系统参数控制器<br/>
 * <p>
 * Description:<br/>
 * <p>
 * 包含以下功能<br/>
 * 查看系统参数列表<br/>
 * 修改系统参数信息<br/>
 * 
 * <p>
 * For Examples <br/>
 * 
 * <PRE>
 *      http://127.0.0.1:8080/upp-admin/sys/parameter/toListView
 * </PRE>
 * <p>
 */
@Controller
@RequestMapping("/sys/parameter")
public class SysParamterController extends AbstractController
{

    @Autowired
    private ConfigureService _configureService;

    /**
     * 
     * 展示系统参数列表 <br/>
     * <p>
     * Description: 点击系统参数菜单时，展示系统参数列表 <br/>
     * <p>
     * Author: yangzq<br/>
     * <p>
     * Date: 2013-5-2-上午11:03:26<br/>
     * <p>
     * 
     * @return
     * 
     */
    @RequestMapping("/toListView")
    public ModelAndView toListView()
    {
        return new ModelAndView("sys/parameter/parameter_list");
    }

    /**
     * 
     * 获取系统参数列表(异步请求) <br/>
     * <p>
     * Description: 调用远程服务获取系统参数列表，返回系统参数列表 <br/>
     * <p>
     * Author: yangzq<br/>
     * <p>
     * Date: 2013-5-2-上午11:21:02<br/>
     * <p>
     * 
     * @return
     * 
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> List(String keywords)
    {
        Map<String, Object> respMap = new HashMap<String, Object>(3);
        try
        {
            PaginationList<SysParameter> paginationList = _configureService.listAll(keywords, getPage(), getPageSize());
            if (paginationList != null)
            {
                respMap.put("total", paginationList.getTotalRecord());
                respMap.put("rows", paginationList.getRecords());
            }
        }
        catch (UppException e)
        {
            respMap.put("total", 0);
            handleException(e);
        }
        catch (Exception e)
        {
            respMap.put("total", 0);
            handleException(e);
        }

        return respMap;
    }

    /**
     * 
     * 显示单个系统参数的详细信息 <br/>
     * <p>
     * Description: 根据传入参数key，显示属性key等于参数key的系统参数的详细信息 <br/>
     * <p>
     * Author: yangzq<br/>
     * <p>
     * Date: 2013-5-2-下午3:19:28<br/>
     * <p>
     * 
     * @param key
     * @return
     * 
     */
    @RequestMapping("/toEditView")
    public ModelAndView toEditView(String key)
    {
        ModelAndView modelAndView = new ModelAndView("sys/parameter/parameter_edit");
        try
        {
            modelAndView.addObject("paramter", _configureService.findByParaKey(key));
        }
        catch (UppException e)
        {
            handleException(e);
        }
        catch (Exception e)
        {
            handleException(e);
        }
        return modelAndView;
    }

    /**
     * 
     * 修改系统参数信息(异步请求) <br/>
     * <p>
     * Description: 根据用户输入，调用远程服务修改系统参数信息 <br/>
     * <p>
     * Author: yangzq<br/>
     * <p>
     * Date: 2013-5-3-上午10:45:31<br/>
     * <p>
     * 
     * @param parameter
     * @return
     * 
     */
    @RequestMapping("/update")
    @ResponseBody
    public Map<String, String> update(SysParameter parameter)
    {
        if (!this.hasPermission(ModuleEnum.SysParam.update))// 没有权限
        {
            _log.error("权限不足");
            return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "权限不足");
        }
        Map<String, String> respMap = null;
        try
        {
            _configureService.updateParamValueByKey(parameter.getKey(), parameter.getValue());
            respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO,
                    ERROR_VALUE_OP_RESULT_SUCCESS_NAME, null);
            addLog(SysParam.update,
                    "operatorId=" + getOperatorId() + "|desc=系统参数修改成功|parameter=" + parameter.toString());
        }
        catch (UppException e)
        {
            respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getName(), null);
        }
        catch (Exception e)
        {
            respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_ERROR, null, "更新失败");
        }
        return respMap;
    }

    /*
     * (non-Javadoc) Description: <br/>
     * 
     * @return
     * 
     * @see com.bcinfo.upp.admin.controller.AdminController#getMouduleCode()
     */
    @Override
    public String getMouduleCode()
    {
        return SysParam.moduleName.value;
    }

}
